<?php
//error_reporting(E_ALL ^ E_NOTICE);

define('InTBak', TRUE);
define('TBAK_PATH', substr(dirname(__FILE__), 0, - 7));
$editor = 0;

require_once TBAK_PATH . 'include/dbchar.php';
require_once TBAK_PATH . 'include/config.php';

// 超时设置
if ($php_outtime) {
    $php_outtime = (int) $php_outtime;
    @set_time_limit($php_outtime);
}

/**
 * 连接数据库
 * @return unknown
 */
function db_connect()
{
    global $db_server, $db_username, $db_password, $db_dbname, $db_port, $db_char, $db_version, $editor, $fun_r;
    $dblocalhost = $db_server;
    // 端口
    if ($db_port) {
        $dblocalhost .= ":" . $db_port;
    }
    $link = @mysql_connect($dblocalhost, $db_username, $db_password);
    // mysql_select_db($db_dbname);
    if (empty($link)) {
        if (empty($fun_r['ConntConnectDb'])) {
            if ($editor == 1) {
                $a = "../";
            } elseif ($editor == 2) {
                $a = "../../";
            } elseif ($editor == 3) {
                $a = "../../../";
            } else {
                $a = "";
            }
            @include_once $a . LoadLang('f.php');
        }
        echo $fun_r['ConntConnectDb'];
        exit();
    }
    // 编码
    DoSetDbChar($db_char);
    if ($db_version >= '5.0') {
        @mysql_query("SET sql_mode=''");
    }
    return $link;
}

/**
 * 关闭数据库连接
 */
function db_close()
{
    global $link;
    @mysql_close($link);
}

/**
 * 设置编码
 * @param unknown $dbchar
 */
function DoSetDbChar($dbchar)
{
    if ($dbchar && $dbchar != 'auto') {
        // @mysql_query("set names '".$dbchar."';");
        @mysql_query('set character_set_connection=' . $dbchar . ',character_set_results=' . $dbchar . ',character_set_client=binary;');
    }
}

/**
 * 取得mysql版本(数据库)
 * @return Ambigous <string, unknown>
 */
function GetMysqlVerForDb()
{
    $sql = mysql_query("select version() as version");
    $r = mysql_fetch_array($sql);
    return ReturnMysqlVer($r['version']);
}

/**
 * 返回mysql版本
 * @param unknown $dbver
 * @return string
 */
function ReturnMysqlVer($dbver)
{
    if (empty($dbver)) {
        return '';
    }
    if ($dbver >= '6.0') {
        $dbver = '6.0';
    } elseif ($dbver >= '5.0') {
        $dbver = '5.0';
    } elseif ($dbver >= '4.1') {
        $dbver = '4.1';
    } else {
        $dbver = '4.0';
    }
    return $dbver;
}

/**
 * 设置COOKIE
 * @param unknown $var
 * @param unknown $val
 * @param number $life
 * @return boolean
 */
function esetcookie($var, $val, $life = 0)
{
    global $cookiedomain, $cookiepath, $cookievarpre;
    return setcookie($cookievarpre . $var, $val, $life, $cookiepath, $cookiedomain);
}

/**
 * 返回cookie
 * @param unknown $var
 * @return unknown
 */
function getcvar($var)
{
    global $cookievarpre;
    $tvar = $cookievarpre . $var;
    return $_COOKIE[$tvar];
}

/**
 * 导入语言包
 * @param unknown $file
 * @return string
 */
function LoadLang($file)
{
    return "templates/pub/" . $file;
}

/**
 * 参数处理函数
 * @param unknown $val
 * @return string
 */
function RepPostVar($val)
{
    $val = str_replace(" ", "", $val);
    $val = str_replace("'", "", $val);
    $val = str_replace("\"", "", $val);
    $val = addslashes(stripSlashes($val));
    return $val;
}

/**
 * 导入模板
 * @param unknown $file
 * @return string
 */
function LoadAdminTemp($file)
{
    return "templates/temp/" . $file;
}

/**
 * 使用编码
 */
function HeaderIeChar()
{
    @header('Content-Type: text/html; charset=utf-8');
}
